package com.beigai.imessage.user.action;

import com.beigai.imessage.client.dto.IPAddress;
import com.beigai.imessage.common.util.IPAddressUtil;
import com.beigai.imessage.dao.mapper.ext.UserFriendPOMapperExt;
import com.beigai.imessage.dao.mapper.ext.UserPOMapperExt;
import com.beigai.imessage.server.SessionMessage;
import com.beigai.imessage.server.UserSession;
import com.beigai.imessage.session.MemorySessionCache;
import com.beigai.imessage.user.dto.UserCookie;
import com.beigai.imessage.user.dto.UserFriend;
import com.beigai.imessage.user.dto.UserInfo;
import com.beigai.imessage.user.dto.UserSignToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xxl on 2017/5/10.
 */
@RestController
public class SignAction {
    @Autowired
    private UserPOMapperExt userPOMapperExt;

    @Autowired
    private UserFriendPOMapperExt userFriendPOMapperExt;

    /**
     * 登录
     * @param token
     * @param req
     * @param response
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    UserCookie login(@RequestBody UserSignToken token, HttpServletRequest req, HttpServletResponse response) {
//        获取ip
        String ipAddr = IPAddressUtil.getIpAddr(req);
//        获取用户信息
        UserInfo userInfo = findUser(token);
//        获取好友列表
        List<UserFriend> userFriends = findFriends(userInfo.getCode());
//        获取session
        MemorySessionCache memorySessionCache = MemorySessionCache.getInstance();
//        设置usersession
        UserSession userSession = new UserSession(memorySessionCache);
        String code = userInfo.getCode();
        userSession.setNickname(userInfo.getNickname());
        userSession.setCode(code);
        Map<String, UserFriend> friends = new HashMap<String, UserFriend>();
        for (UserFriend friend :
                userFriends) {
            friends.put(friend.getCode(), friend);
        }
        userSession.setFriends(friends);
//        获取好友登录状态
        for (UserFriend userFriend : userFriends) {
            UserSession session = memorySessionCache.getSession(userFriend.getCode());
            if (session != null)
                userFriend.setOnline(true);
        }
        userSession.setIpAddress(new IPAddress(ipAddr, 6888));
//        添加usersession到session
        memorySessionCache.addSession(code, userSession);
//        创建中介消息
        SessionMessage sessionMessage = new SessionMessage(code, SessionMessage.ONLINE, true);
//        发送中介消息
        userSession.contact(sessionMessage);
//        写cookie
        response.addCookie(new Cookie("sessionId", code));
        UserCookie userCookie = new UserCookie();
        userCookie.setCode(code);
        return userCookie;
    }

    private List<UserFriend> findFriends(String code) {
        List<UserFriend> userFriends = userFriendPOMapperExt.findFriends(code);
        return userFriends;
    }

    private UserInfo findUser(UserSignToken token) {
        UserInfo userInfo = userPOMapperExt.selectByCondition(token);
        return userInfo;
    }

    /**
     * 登出
     * @param code
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    void logout(String code) {
        MemorySessionCache memorySessionCache = MemorySessionCache.getInstance();
        UserSession session = memorySessionCache.getSession(code);
        if (session == null)
            return;
        session.contact(new SessionMessage(code, SessionMessage.OFFLINE, false));
        memorySessionCache.removeSession(code);
    }
}
